﻿Imports System.Windows.Forms
Imports ArfaUI

Public Class SettingPosisiMejaPage
    Inherits PageTemplate

    Private drag As Boolean
    Private btn As Button ' this is a reference object
    Private ptX, ptY As Integer

    Private Sub RefreshTabel()
        Dim dt As New DataTable
        If Utils.executeSP("proc_tabel", New Object() {"select", 0, 0, 0, 0}, dt) Then
            If dt IsNot Nothing Then
                If dt.Rows.Count > 0 Then
                    For Each dr As DataRow In dt.Rows
                        Dim nbtn As New Button
                        If dr("aktif").ToString = "1" Then
                            nbtn.BackColor = Color.Green
                        Else
                            nbtn.BackColor = Color.CornflowerBlue
                        End If

                        nbtn.Size = New System.Drawing.Size(72, 59)
                        nbtn.Text = dr("idtabel").ToString
                        nbtn.Name = dr("idtabel").ToString
                        nbtn.Location = New Point(dr("xpos"), dr("ypos"))
                        AddHandler nbtn.MouseDown, AddressOf DownEvent
                        AddHandler nbtn.MouseUp, AddressOf UpEvent
                        AddHandler nbtn.MouseMove, AddressOf MoveEvent

                        Dim ctlAll As Control()
                        ctlAll = PanButton.Controls.Find(dr("idtabel").ToString, False)
                        If ctlAll.Length > 0 Then
                            Dim ctldim As Button
                            ctldim = CType(ctlAll(0), Button)
                            ctldim.Location = New Point(dr("xpos"), dr("ypos"))
                            PanButton.Refresh()
                        Else
                            PanButton.Controls.Add(nbtn)
                        End If

                    Next
                End If
            End If
        End If
    End Sub

    Private Sub MoveEvent(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If drag Then
            btn.Location = New Point(btn.Location.X + e.X - ptX, btn.Location.Y + e.Y - ptY)
            Me.Refresh()
            PanButton.Refresh()
        End If
    End Sub
    Private Sub UpEvent(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        drag = False
    End Sub
    Private Sub DownEvent(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = MouseButtons.Left Then
            drag = True
            btn = CType(sender, Button)
            ptX = e.X : ptY = e.Y
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        For Each ctl In PanButton.Controls
            If TypeOf ctl Is Button Then
                Dim ctldim As New Button
                ctldim = CType(ctl, Button)
                If Utils.exec_SP("proc_tabel", New Object() {"edit", ctldim.Text, ctldim.Location.X, ctldim.Location.Y, 0}) Then

                End If
            End If
        Next
        MsgBox("Execute Finish")
        RefreshTabel()
    End Sub

    Private Sub SettingPosisiMejaPage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshTabel()
    End Sub

    Private Sub SettingPosisiMejaPage_VisibleChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.VisibleChanged
        RefreshTabel()
    End Sub
End Class
